$(function(){
   //查询采购状态
   var buyerStatus = requestData(path + "/buyer-status");
   //查询审核状态
   var checkStatus = requestData(path + "/check-status");
   var data = {
		method: 'GET',
		url:path+'/procurement/procurement-plan',
		contentType: 'application/x-www-form-urlencoded',
		columns: [{
			field: 'checked',
			checkbox: true
		}, {
			field: 'procurePlanId',
			visible: false
		}, {
			field: 'procurePlanOrderNumber',
			title: '采购单号'
		}, {
			field: 'procurePlanTheme',
			title: '采购主题'
		}, {
			field: 'procurePlanPlanner',
			title: '计划员'
		}, {
			field: 'procurePlanBuyer',
			title: '采购员'
		}, {
			field: 'procurePlanAmountBudget',
			title: '金额预算',
			sortable: true
		}, {
			field: 'procurePlanStartDate',
			title: '采购日期',
			sortable: true
		}, {
			field: 'procurePlanEndDate',
			title: '截止日期'
		}, {
			field: 'procurePlanBuyerStatus',
			title: '采购状态',
			formatter: function (value, row, index) {
				if(buyerStatus[value-1].buyerStatusStyle!="red"){
					return '<span class="layui-badge layui-bg-'+buyerStatus[value-1].buyerStatusStyle+'">'
					+buyerStatus[value-1].buyerStatusName+'</span>';
				}
				return '<span class="layui-badge">'+buyerStatus[value-1].buyerStatusName+'</span>'
			}
		}, {
			field: 'procurePlanCheckStatus',
			title: '审批状态',
			formatter: function (value, row, index) {
				if(checkStatus[value-1].checkStatusStyle!="red"){
					return '<span class="layui-badge layui-bg-'+checkStatus[value-1].checkStatusStyle+'">'
					+checkStatus[value-1].checkStatusName+'</span>';
				}
				return '<span class="layui-badge">'+checkStatus[value-1].checkStatusName+'</span>'
            }
		}],	
		sortable: true,
        sortOrder: "asc",
        search: false,
   }
   
   var param = [{text:"采购单号",name:"procurePlanOrderNumber",type:"text",verify:"intNumber"},{text:"计划员",name:"procurePlanPlanner",type:"text",verify:"peopleName"},{text:"采购员",name:"procurePlanBuyer",type:"text",verify:"peopleName"},{text:"日期范围",name:"dateScope",type:"text",verify:""},{text:"采购状态",name:"procurePlanBuyerStatus",type:"select",verify:"",option:[{key:"1",value:"等待采购"},{key:"2",value:"采购中"},{key:"3",value:"采购完成"},{key:"4",value:"采购出错"},{key:"5",value:"重新采购"}]},{text:"审批状态",name:"procurePlanCheckStatus",type:"select",verify:"",option:[{key:"1",value:"等待审核"},{key:"2",value:"审核中"},{key:"3",value:"审核通过"},{key:"4",value:"审核不通过"},{key:"5",value:"重新审核"}]}];
   
   renderTable(cTable,data);
   
   createQueryPanel(param);
   
   laydate.render({
	   elem: '[name="dateScope"]'
	   ,range: true
   });
   
   form.render();
})

//添加按钮
$("#add_btn").click(function(){
	openOperateWindow({
		title: "添加采购计划",
		full: true,
		url: "/admin/enterpriseManagement/procurementManagement/procurementPlan/procurement-plan-add.jsp"
	})
})

//删除按钮
$("#remove_btn").click(function(){
	requestRemoveData("procurePlanId","/procurement/procurement-plan");
})

//修改按钮
$("#modify_btn").click(function(){
	
	openOperateWindow({
		title: "修改采购计划",
		full: true,
		url: "/admin/enterpriseManagement/procurementManagement/procurementPlan/procurement-plan-update.jsp"
	})
})

//审核按钮
$("#check_status_btn").click(function(){
	var selectedObj = cTable.bootstrapTable('getSelections');
	if(selectedObj[0].procurePlanCheckStatus==="3"){
		fixedAlert("该计划已经审核通过,无需再次审核!",0);
	}else if(selectedObj.length==1){
		layer.open({
			type: 2,
			area: ['85%', '85%'],
			title: "审核",
			fixed: false, //不固定
			maxmin: true,
			content: path + '/admin/enterpriseManagement/procurementManagement/procurementPlan/procurement-plan-check.jsp',
			success: function(){
			  //当状态是等待审核时才发送更新请求
			  if(selectedObj[0].procurePlanCheckStatus == "1"){
				  selectedObj[0].procurePlanCheckStatus = 2;
				  //点击查看后将审核状态修改为审核中
				  $.ajax({
					   type: 'PUT',
					   async: false,
					   url: path+"/procurement/procurement-plan",
					   data: JSON.stringify(selectedObj[0]),
					   contentType: "application/json;charset='UTF-8'", 
				  });
			  }
			} 
		})
	}else{
		fixedAlert("请选择一条需要审核的计划!",0);
	}
})

//采购按钮
$("#buyer_status_btn").click(function(){
	var selectedObj = cTable.bootstrapTable('getSelections');
	if(selectedObj.length==1&&selectedObj[0].procurePlanCheckStatus=="3"){
		layer.open({
		  type: 2,
		  area: ['30%', '35%'],
		  title: "采购",
		  fixed: false, //不固定
		  maxmin: true,
		  content: path + '/admin/enterpriseManagement/procurementManagement/procurementPlan/procurement-plan-buyer.jsp'
		})
	}else{
		fixedAlert("请选择一条需要修改的采购计划!必须审核通过 才可以修改该采购单!",0);
	}
})

//查看产品
$("#query_selected_product_btn").click(function(){
	openOperateWindow({
		title: "查看产品",
		iframeAuto: true,
		url: "/admin/enterpriseManagement/procurementManagement/procurementPlan/procurement-plan-query.jsp"
	});
})
